library(tidyverse)
library(randomForest)
library(randomForestExplainer)

#### Read in data with necessary data for each sample. In this tutorial the column headings must be the same as those used when the model was created.
SHdata <-read.csv('2024 Tutorial samples to use RF model file.csv')


#### Load AWC Full Random Forest Model File:
load("availablewatercapacity_full_randomforestmodel_2023.rda")
#Note the Model File Name in R is RFAWC


#### Use AWC Full Random Forest Model to predict AWC on samples of interest.
SHdata$AWC.predict = predict(RFAWC, SHdata)


#### Load Field Capacity Full Random Forest Model File:
load("fieldcapacity_full_randomforestmodel_2023.rda")
#Note the Model File Name in R is RF0.1Bar (0.1 Bar is equivalent to 10kPa)

#### Use Field Capacity Full Random Forest Model to predict field capacity on samples of interest.
SHdata$FC.predict = predict(RF0.1Bar, SHdata)


#### Load Permanent Wilting Point Full Random Forest Model File:
load("permanentwiltingpoint_full_randomforestmodel_2023.rda")
#Note the Model File Name in R is RF15Bar (15 Bar is equivalent to 1500kPa)

#### Use Permanent Wilting Point Full Random Forest Model to predict permanent wilting point on samples of interest.
SHdata$PWP.predict = predict(RF15Bar, SHdata)

### NOTE: Predicting AWC directly may give you slightly different results than predicting FC and PWP separately and calculating AWC, but both methods provide comparable accuracy.



#### Code to load Reduced Random Forest Models (these models only require sand, silt, clay, and OM):

#### Load AWC Reduced Random Forest Model File:
load("availablewatercapacity_reduced_randomforestmodel_2023.rda")
#Note the Model File Name in R is RFAWC.reduced

#### Load Field Capacity Reduced Random Forest Model File:
load("fieldcapacity_reduced_randomforestmodel_2023.rda")
#Note the Model File Name in R is RFReduced0.1Bar (0.1 Bar is equivalent to 10kPa)

#### Load Permanent Wilting Point Reduced Random Forest Model File:
load("permanentwiltingpoint_reduced_randomforestmodel_2023.rda")
#Note the Model File Name in R is RFReduced15Bar (15 Bar is equivalent to 1500kPa)


